home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
ZIVFILE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-19
|
10KB
|
355 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: zn.cpp
// Title: Zinc Window Template
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains code for the class ZI_VIEW_FILE.
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <zi.hpp>
#define USE_WIN_VIEW_FILE
#if OS_DOS
#include <zid.hpp>
#elif OS_WINDOWS
#include <ziw.hpp>
#else
#include <zio.hpp>
#endif
//----------------------------------------------------------------------------
// Description: Default constructor
// Parameters:
// Returns:
//----------------------------------------------------------------------------
FN_M ZI_VIEW_FILE::ZI_VIEW_FILE(PCSZ _pcszDisplay, PCSZ _pcszPrint, PCSZ _pcszSep)
: ZN_WINDOW("WIN_VIEW_FILE", ZN_LOAD_CENTER|ZN_LOAD_NO_SHOW)
{
ZI_VIEW_FILE::Initialize(CL_INIT_CLASS);
Setup();
Assert(_pcszDisplay);
pcszDisplay = _pcszDisplay;
pcszPrint = _pcszPrint;
pcszSep = _pcszSep ? _pcszSep: "##";
Format();
}
//----------------------------------------------------------------------------
// Description: Destructor
// Parameters:
// Returns:
//----------------------------------------------------------------------------
FN_M ZI_VIEW_FILE::~ZI_VIEW_FILE()
{
ZI_VIEW_FILE::Destroy(FALSE);
Terminate();
}
//----------------------------------------------------------------------------
// Description: Destroy object. Free any resources used by object.
// Normally called by destructor.
// Should allow multiple calls from various classes.
// A class should almost always re-init its variables when
// it is destroyed to prevent accidents.
// Parameters: fDestroyAll Destroy parents also?
// Default is TRUE.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M ZI_VIEW_FILE::Destroy(BOOL fDestroyAll)
{
ZI_VIEW_FILE::Initialize(CL_INIT_CLASS_VARS);
if (fDestroyAll) // Destroy parent.
ZI_VIEW_FILE_PARENT::Destroy(fDestroyAll);
if (pcszTitle)
pcszTitle = (PCSZ)MemFree((PVOID)pcszTitle);
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Set field values.
// Parameters:
// Returns: TRUE if valid
//----------------------------------------------------------------------------
BOOL FN_M ZI_VIEW_FILE::Format()
{
//
// Get and verify the full file name
//
CHAR szFile[MAX_PATH+1];
strcpy(szFile, pcszDisplay);
FnameQualify(szFile, "DOC", EnvGet("PATH"), 0);
if (!FnameIsFile(szFile))
return Error("File '%s' not found.", szFile);
//
// Open file and get size
//
BOOL fResult = FALSE;
FLAG16 fs = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
FPOS fsize;
HF hf = -1;
PBYTE pbBuf = NULL;
PCSZ pcszWinTitle = "File not found.";
PCSZ pcszText = pcszWinTitle;
PSZ psz;
PSZ pszCopy;
BOOL fEol;
SIZET cSep = strlen(pcszSep);
SIZET cBuf;
if (!FileOpen(&hf, szFile, fs, NULL))
goto ERROR_EXIT;
fsize = FileGetSize(hf);
if (fsize < 0)
goto ERROR_EXIT;
if (fsize == 0 || fsize > 30000L)
goto ERROR_EXIT;
//
// Allocate a buffer and read the file into memory
//
pbBuf = (PBYTE)MemAlloc((SIZET)fsize + 1);
if (pbBuf == NULL)
{
ErrorNoMem();
goto ERROR_EXIT;
}
if (!FileRead(hf, pbBuf, (SIZET)fsize, 0))
goto ERROR_EXIT;
pbBuf[(SIZET)fsize] = '\0'; // Be sure to add null terminator
FileClose(hf);
hf = -1;
// Remove Ctrl-Z
if ((psz = strchr((PSZ)pbBuf, '\x1A')) != NULL)
psz[0] = '\0';
//
// Go through the file removing hard linefeeds and inserting
// new one based on the location of separators!!
//
Assert(cSep >= 2);
psz = (PSZ)pbBuf; // Eliminate hard line breaks
pszCopy = (PSZ)pbBuf;
fEol = FALSE;
cBuf = strlen(psz);
while (cBuf)
{
if (cBuf >= cSep && memcmp(psz, pcszSep, cSep) == 0)
{
cBuf -= cSep;
psz += cSep;
*pszCopy++ = '\r';
*pszCopy++ = '\n';
fEol = TRUE;
}
else if (psz[0] == '\r' || psz[0] == '\n')
{
while (psz[0] == '\r' || psz[0] == '\n')
{
psz++;
cBuf--;
}
if (psz[0] && !isspace(psz[0]) && !fEol)
*pszCopy++ = ' ';
fEol = FALSE;
}
else if (psz[0] == '\t')
{
psz++;
cBuf--;
*pszCopy++ = ' ';
fEol = FALSE;
}
else
{
*pszCopy++ = *psz++;
cBuf--;
fEol = FALSE;
}
}
*pszCopy = '\0';
//
// Break out title and get start of text
//
pcszWinTitle = (PCSZ)pbBuf;
psz = strchr(pcszWinTitle, '\r');
if (psz) // Add null terminator and skip to start of text
{
*psz++ = '\0';
while (psz[0] == '\r' || psz[0] == '\n')
psz++;
pcszText = (PCSZ)psz;
}
else
pcszText = (PCSZ)pbBuf;
fResult = TRUE;
if (pcszTitle) // Set global title
pcszTitle = (PCSZ)MemFree((PVOID)pcszTitle);
pcszTitle = MemStrDup(pcszWinTitle);
ERROR_EXIT:
//
// Set window and title!!
//
SetTitle(pcszTitle);
SetText(FID(TEXT), pcszText, strlen(pcszText)+1);
//
// Clean up!
//
if (hf >= 0)
FileClose(hf);
if (pbBuf)
MemFree(pbBuf);
return fResult;
}
//----------------------------------------------------------------------------
// Description: Initialize object.
// Normally called by constructor.
// Should allow multiple calls from various classes.
// Parameters: sInit Initialization code. May be one of the following:
// CL_INIT_CLASS Reset class variables and
// and dynamic allocations for
// this class only.
// CL_INIT_CLASS_VARS Reset class variables for
// this class only.
// CL_INIT_VARS Reset class variables for
// this class only.
// CL_INIT_ALL Initialize class and all
// parent class, including
// dynamic memory allocation.
// Default is CL_INIT_ALL
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_M ZI_VIEW_FILE::Initialize(SHORT sInit)
{
if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
ZI_VIEW_FILE_PARENT::Initialize(sInit);
pcszDisplay = NULL;
pcszPrint = NULL;
pcszSep = NULL;
pcszTitle = NULL;
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Set field values.
// Parameters:
// Returns: TRUE if valid
//----------------------------------------------------------------------------
BOOL FN_M ZI_VIEW_FILE::Print()
{
CHAR szFile[MAX_PATH+1];
if (pcszPrint == NULL || !pcszPrint[0])
return TRUE;
strcpy(szFile, pcszPrint); // Qualify file name
FnameQualify(szFile, "PRN", EnvGet("PATH"), 0);
if (!FnameIsFile(szFile)) // Check if file exists
return Error("File '%s' not found.", szFile);
return PrintFile(szFile, 0, NULL, pcszTitle);
}
//----------------------------------------------------------------------------
// Description: Event monitor function.
// Parameters: msg Event code
// pv1 Data pointer 1
// pv2 Data pointer 2
// Returns: Event code
//----------------------------------------------------------------------------
ZN_MSG FN_M ZI_VIEW_FILE::User(ZN_MSG msg, PVOID, PVOID)
{
switch (msg)
{
case ZN_MSG_INIT:
return msg;
case ZN_MSG_TERMINATE:
return msg;
}
if (IsError()) // Error condition
return msg;
switch (msg)
{
case TB_CLOSE:
Close();
break;
case TB_PRINT:
Print();
break;
case TB_HELP:
break;
}
return msg;
}
//----------------------------------------------------------------------------
// Description:
// Parameters:
// Returns:
//----------------------------------------------------------------------------
BOOL FN_M ZI_VIEW_FILE::ViewFile(PCSZ _pcszDisplay, PCSZ _pcszPrint, PCSZ _pcszSep)
{
PZI_VIEW_FILE pzi_view_file = new ZI_VIEW_FILE(_pcszDisplay, _pcszPrint, _pcszSep);
if (pzi_view_file == NULL)
return _ErrorNoMem();
else if (pzi_view_file->IsValid())
{
pzi_view_file->Show();
return TRUE;
}
return FALSE;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------